package com.suanfa.jiaoheng;

import java.util.Arrays;

public class 第2课选择排序 {
    public static void main(String[] args) {
        int[] numArr = new int[]{7,4,2,6,5,3,8,1};
        selectSort(numArr);
        Arrays.stream(numArr).forEach(value -> System.out.print(value+" "));
    }

    public static void selectSort(int[] numArr){
        for(int thisTurnPos = 0; thisTurnPos<numArr.length-1;thisTurnPos++){
            // 找到这一轮比较重值比较小的位置
            int minerPos = thisTurnPos;
            for(int comparePos = thisTurnPos+1;comparePos<=numArr.length-1;comparePos++){
                if(numArr[minerPos]>numArr[comparePos]){
                    minerPos = comparePos;
                }
            }
            // 交换小值和大值的位置
            int thisTurnVal = numArr[thisTurnPos];
            numArr[thisTurnPos] = numArr[minerPos];
            numArr[minerPos] = thisTurnVal;
        }
    }
}
